package handler

import (
	"google.golang.org/protobuf/types/dynamicpb"
	"sen-golang-study/go_game_study/hero_story_go_server/biz_server/service/user/userdata"
	"sen-golang-study/go_game_study/hero_story_go_server/common/log"
	"sen-golang-study/go_game_study/hero_story_go_server/common/server_msg/proto_msg"
)

// 处理谁在场
func handleWhoElseIsHere(ctx CmdContext, _ *dynamicpb.Message) {
	if ctx == nil || ctx.GetUserId() <= 0 {
		return
	}

	log.Info("收到“还有谁在场”的消息！userId = %d", ctx.GetUserId())

	whoElseIsHereResult := &proto_msg.WhoElseIsHereResult{}

	allUsers := userdata.GetUserGroup().GetAllUserMap()

	for _, user := range allUsers {
		if user == nil {
			continue
		}

		userInfo := &proto_msg.WhoElseIsHereResult_UserInfo{
			UserId:     user.UserId,
			UserName:   user.UserName,
			HeroAvatar: user.HeroAvatar,
		}

		if user.MoveState != nil {
			userInfo.MoveState = &proto_msg.WhoElseIsHereResult_UserInfo_MoveState{
				FromPosX:  user.MoveState.FromPosY,
				FromPosY:  user.MoveState.FromPosY,
				ToPosX:    user.MoveState.ToPosX,
				ToPosY:    user.MoveState.ToPosY,
				StartTime: uint64(user.MoveState.StartTime),
			}
		}

		whoElseIsHereResult.UserInfo = append(
			whoElseIsHereResult.UserInfo,
			userInfo,
		)
	}

	ctx.Write(whoElseIsHereResult)
}
